package com.venta.autopartes.utils.ajax;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.venta.autopartes.car.Country;
import com.venta.autopartes.car.Location;
import com.venta.autopartes.car.State;
import com.venta.autopartes.car.Suburb;
import com.venta.autopartes.car.Town;
import com.venta.autopartes.utils.DataCatalog;
import com.venta.autopartes.utils.SessionFactory;

public class LocationsRetriver extends ActionSupport implements SessionAware{
	private Object jsonModel;
	private int countryId;
	private int stateId;
	private int townId;
	private int suburbId;
	private int zipcode;
	private Map session;
	

	public String execute() {
		
		Country selectedCountry=null;
		State selectedState=null;
		Town selectedTown=null;
		Suburb selectedSuburb=null;
		DataCatalog dataCatalog=(DataCatalog)session.get("dataCatalog");
		Location location=dataCatalog.getLocations();
		
		if(countryId>0){
			System.out.println("Selected country: " + countryId);
			List<Country> countryList=location.getCountriesList();
			for(Country country:countryList){
				if(country.getCountryId()==countryId){
					selectedCountry=country;
				}
			}
			session.put("selectedCountry", selectedCountry);
			setJsonModel(selectedCountry);
		}else if(stateId>0){
			System.out.println("Selected state: "+stateId);
			selectedCountry=(Country)session.get("selectedCountry");
			List<State> stateList=selectedCountry.getStates();
			for(State state:stateList){
				if(state.getStateId()==stateId){
					selectedState=state;
				}
			}
			session.put("selectedState", selectedState);
			setJsonModel(selectedState);
		}else if(townId>0){
			System.out.println("Selected town: "+townId);
			selectedState=(State)session.get("selectedState");
			List<Town> townList=selectedState.getTowns();
			for(Town town:townList){
				if(town.getTownId()==townId){
					selectedTown=town;
				}
			}
			session.put("selectedTown", selectedTown);
			setJsonModel(selectedTown);
		}else if(suburbId>0){
			System.out.println("Selected suburb: "+suburbId);
			selectedTown=(Town)session.get("selectedTown");
			List<Suburb> suburbList=selectedTown.getSuburbs();
			for(Suburb suburb:suburbList){
				if(suburb.getSuburbId()==suburbId){
					selectedSuburb=suburb;
				}
			}
			//SessionFactory.removeCountrySelection();
			System.out.println(selectedSuburb);
			setJsonModel(selectedSuburb);
		}
		return SUCCESS;
	}


	public Object getJsonModel() {
		return jsonModel;
	}


	public void setJsonModel(Object jsonModel) {
		this.jsonModel = jsonModel;
	}


	public int getCountryId() {
		return countryId;
	}


	public void setCountryId(int countryId) {
		this.countryId = countryId;
	}


	public int getStateId() {
		return stateId;
	}


	public void setStateId(int stateId) {
		this.stateId = stateId;
	}


	public int getTownId() {
		return townId;
	}


	public void setTownId(int townId) {
		this.townId = townId;
	}


	public int getSuburbId() {
		return suburbId;
	}


	public void setSuburbId(int suburbId) {
		this.suburbId = suburbId;
	}
	
	


	public int getZipcode() {
		return zipcode;
	}


	public void setZipcode(int zipcode) {
		this.zipcode = zipcode;
	}


	public Map getSession() {
		return session;
	}


	public void setSession(Map session) {
		this.session = session;
	}
	
	
	
}
